package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 李智勇
 * @data 2025/10/8 11:32
 */
@Slf4j
@RestController
@RequestMapping(path = "/users")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/dept/{deptId}")
    public Result listByDept(@PathVariable Integer deptId) {
        return Result.success(userService.listByDept(deptId));
    }
    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel) {
        return Result.success(userService.listByRole(roleLabel));
    }
    @GetMapping("/list")
    public Result getUserList(UserDTO dto){
        PageResult<UserDO> userPageResult = userService.list(dto);
        return Result.success(userPageResult);
    }
    @LogAnno
    @PutMapping
    public Result updateUser(@RequestBody User user) {
        log.info("修改用户: {}", user);
        userService.updateUser(user);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getUserById(@PathVariable("id") Integer id) {
        log.info("根据ID查询用户: {}", id);
        User user = userService.getUserById(id);
        return Result.success(user);
    }
    @LogAnno
    @DeleteMapping("/{ids}")
    public Result deleteUsers(@PathVariable("ids") List<Integer> ids) {
        log.info("批量删除用户: {}", ids);
        userService.deleteUsers(ids);
        return Result.success();
    }
    @LogAnno
    @PostMapping
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }

    @GetMapping
    public Result list(UserDTO dto){
        PageResult<UserDO> pageResult = userService.list(dto);
        return Result.success(pageResult);
    }
}
